package com.huluobo.a2209mdmdemo.demo

import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.offset
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.pager.HorizontalPager
import androidx.compose.foundation.pager.rememberPagerState
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.Button
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.unit.dp
import coil.compose.AsyncImage

/**
 *  Created by LC on 2025/2/22.
 */
@OptIn(ExperimentalFoundationApi::class)
@Composable
fun Demo05() {

    val pagerState = rememberPagerState {
        5
    }

    //控制弹窗是否显示
    var isShow by remember {
        mutableStateOf(false)
    }


    val list = listOf(
        "https://img2.baidu.com/it/u=1134184213,2435039814&fm=253&fmt=auto?w=1062&h=800",
        "https://img1.baidu.com/it/u=2693733305,4035903587&fm=253&app=138&size=w931&n=0&f=JPEG&fmt=auto?sec=1729184400&t=92694ac7a4bb0b4a405af0e28c18cd82",
        "https://img0.baidu.com/it/u=540669065,2162680038&fm=253&app=120&size=w931&n=0&f=JPEG&fmt=auto?sec=1729184400&t=3adc1ec41d0f0d8ac469eb88eb19c531",
        "https://img2.baidu.com/it/u=553712968,3437602612&fm=253&app=120&size=w931&n=0&f=JPEG&fmt=auto?sec=1729184400&t=280fc51f8b1c0619aedbe9c1d5ce9895",
        "https://img0.baidu.com/it/u=1677400077,1401427640&fm=253&fmt=auto?w=679&h=1216"
    )

    Column(modifier = Modifier.height(300.dp)) {
        //横向的ViewPager
        HorizontalPager(state = pagerState, modifier = Modifier.height(250.dp), verticalAlignment = Alignment.Top) {
            AsyncImage(
                model = list[it],
                contentDescription = "",
                contentScale = ContentScale.Crop,
                modifier = Modifier.height(250.dp)
            )
        }
        Row(
            horizontalArrangement = Arrangement.Center, modifier = Modifier
                .fillMaxWidth()
                //向上偏移40dp高度
                .offset(y = (-40).dp)
        ) {
            //重复,获取pager数量,类似横向的RecyclerView
            repeat(pagerState.pageCount) {
                //如果当前选中的页面等于重复中的index,修改指示器的颜色
                val color = if (pagerState.currentPage == it) Color.DarkGray else Color.LightGray
                Box(
                    modifier = Modifier
                        .size(16.dp)
                        .padding(3.dp)
                        .background(color = color, CircleShape)
                )
            }
        }

        Button(onClick = { isShow = true }) {
            Text(text = "删除弹窗")
        }
    }

    if (isShow) {
        AlertDialog(
            //当单击弹窗外的范围时
            onDismissRequest = {
                isShow = false
            },
            //确认按钮
            confirmButton = {
                Text(text = "确定", Modifier.clickable {
                    isShow = false
                })
            },
            //取消按钮
            dismissButton = {
                Text(text = "取消", Modifier.clickable {
                    isShow = false
                })
            },
            //弹窗标题
            title = {
                Text(text = "删除提醒")
            },
            //弹窗的message
            text = {
                Text(text = "是否确定删除?")
            }
        )
    }
}